package com.mt.vsphere.business.vsphere;

import com.mt.vsphere.business.bean.base.VCenterCatalogue;
import com.mt.vsphere.business.bean.enums.SysCatalogueType;
import com.vmware.vim25.*;

import javax.xml.ws.soap.SOAPFaultException;
import java.util.ArrayList;
import java.util.List;

public class VCenterCatalogueUtil {

    public static List<VCenterCatalogue> getVCenterCatalogue(VCenterDataHelper vCenterDataHelper){

        return getAllDataCenters(vCenterDataHelper);
    }

    public static List<VCenterCatalogue> getAllDataCenters(VCenterDataHelper vCenterDataHelper){
        List<VCenterCatalogue> list = new ArrayList<>();

        ManagedObjectReference rootFolder = vCenterDataHelper.vCenterConnectHelper.serviceContent.getRootFolder();
        try {
            TraversalSpec traversalSpec = new TraversalSpec();
            traversalSpec.setName("VisitFolders");
            traversalSpec.setType("Folder");
            traversalSpec.setPath("childEntity");
            traversalSpec.setSkip(false);


            PropertySpec propertySpec = new PropertySpec();
            propertySpec.setAll(Boolean.FALSE);
            propertySpec.getPathSet().add("name");
            propertySpec.setType("Datacenter");

            ObjectSpec objectSpec = new ObjectSpec();
            objectSpec.setObj(rootFolder);
            objectSpec.setSkip(Boolean.TRUE);
            objectSpec.getSelectSet().add(traversalSpec);

            for (ObjectContent objectContent:VCenterMobHelper.getResult(vCenterDataHelper.vCenterConnectHelper,objectSpec,propertySpec)){
                ManagedObjectReference mob = objectContent.getObj();
                vCenterDataHelper.mobMap.put(mob.getType()+"@"+mob.getValue(),mob);
                list.add(new VCenterCatalogue(mob.getType(),mob.getValue(), SysCatalogueType.Datacenter.num,getPropName(objectContent),getAllComputeResource(vCenterDataHelper,mob)));

            }

        } catch (SOAPFaultException sfe) {
            VCenterMobHelper.printSoapFaultException(sfe);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    private static  String getPropName(ObjectContent objectContent){
        StringBuilder name = new StringBuilder();

        objectContent.getPropSet().forEach(item->{
            objectContent.getPropSet().forEach(item1->{
                if ("name".equals(item1.getName())){
                    name.append(item1.getVal());
                }
            });
        });
        return name.toString();
    }


    private static List<VCenterCatalogue> getAllComputeResource(VCenterDataHelper vCenterDataHelper,ManagedObjectReference datecenterRefer) throws Exception {
        List<VCenterCatalogue> list = new ArrayList<>();
        TraversalSpec dataCenterToHostFolder = new TraversalSpec();
        dataCenterToHostFolder.setName("DataCenterToVMFolder");
        dataCenterToHostFolder.setType("Datacenter");
        dataCenterToHostFolder.setPath("hostFolder");
        dataCenterToHostFolder.setSkip(false);

        TraversalSpec traversalSpec = new TraversalSpec();
        traversalSpec.setName("VisitFolders");
        traversalSpec.setType("Folder");
        traversalSpec.setPath("childEntity");

        dataCenterToHostFolder.getSelectSet().add(traversalSpec);


        ObjectSpec objectSpec = new ObjectSpec();
        objectSpec.setObj(datecenterRefer);
        objectSpec.setSkip(Boolean.TRUE);
        objectSpec.getSelectSet().add(dataCenterToHostFolder);

        PropertySpec propertySpec = new PropertySpec();
        propertySpec.setAll(Boolean.FALSE);
        propertySpec.getPathSet().add("name");
        propertySpec.setType("ComputeResource");


        for (ObjectContent objectContent:VCenterMobHelper.getResult(vCenterDataHelper.vCenterConnectHelper,objectSpec,propertySpec)){
            ManagedObjectReference mob = objectContent.getObj();
            if ("ClusterComputeResource".equals(mob.getType())){
                //如果是集群那么寻找他下面所有的主机
                vCenterDataHelper.mobMap.put(mob.getType()+"@"+mob.getValue(),mob);
                list.add(new VCenterCatalogue(mob.getType(),mob.getValue(), SysCatalogueType.ClusterComputeResource.num,getPropName(objectContent),getAllHostInClusterComputeResource(vCenterDataHelper,mob)));
            }else if ("HostSystem".equals(mob.getType())){
                vCenterDataHelper.mobMap.put(mob.getType()+"@"+mob.getType(),mob);
                list.add(new VCenterCatalogue(mob.getType(),mob.getValue(), SysCatalogueType.HostSystem.num,getPropName(objectContent),getAllVmInHost(vCenterDataHelper,mob)));
            }

        }
        return list;
    }

    //获取集群下面的所有主机
    public static List<VCenterCatalogue> getAllHostInClusterComputeResource(VCenterDataHelper vCenterDataHelper,ManagedObjectReference clusterComputeResourceObj) throws Exception {
        List<VCenterCatalogue> list = new ArrayList<>();
        List<ObjectContent>  contentList = VCenterMobHelper.getAttrInObj(vCenterDataHelper.vCenterConnectHelper,clusterComputeResourceObj,"host","HostSystem");
        for (ObjectContent objectContent:contentList){
            ManagedObjectReference mob = objectContent.getObj();
            vCenterDataHelper.mobMap.put(mob.getType()+"@"+mob.getValue(),mob);
            list.add(new VCenterCatalogue(mob.getType(),mob.getValue(), SysCatalogueType.HostSystem.num,
                    getPropName(objectContent),getAllVmInHost(vCenterDataHelper,objectContent.getObj())));

        }
        return list;
    }

    //获取集群下面的所有主机
    public static List<VCenterCatalogue> getAllVmInHost(VCenterDataHelper vCenterDataHelper,ManagedObjectReference hostObj) throws Exception {
        List<VCenterCatalogue> list = new ArrayList<>();
        List<ObjectContent>  contentList = VCenterMobHelper.getAttrInObj(vCenterDataHelper.vCenterConnectHelper,hostObj,"vm","VirtualMachine","name", "summary");
        for (ObjectContent objectContent:contentList){
            ManagedObjectReference mob = objectContent.getObj();
            vCenterDataHelper.mobMap.put(mob.getType()+"@"+mob.getValue(),mob);
            list.add(new VCenterCatalogue(mob.getType(),mob.getValue(), SysCatalogueType.VirtualMachine.num,
                    getPropName(objectContent),null,mob));
        }
        return list;
    }


    //获取所有的数据
    // 包括Vcenter的目录结构 数据中心->群集 主机->Vapp 虚拟机
    // 获取每台主机的信息 获取每台是虚拟机的信息
    //






}
